/**
 * 求最大气温的Mapper，主要提取数据的功能
 */
package com.tjh.hadoop.mapred;

import java.io.IOException;
import java.net.InetAddress;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

/**
 * @author jahen
 * @date 2016年9月15日
 * @time 下午9:25:31
 * @category 
 * TODO 
 * 这里map函数的功能是，进行数据提取年份、气温数据
 */
public class MyMaxTempMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
	//
	private static final int MISSING = 9999;

	/**
	 * mapper
	 */
	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
			throws IOException, InterruptedException {
		
		InetAddress ip = InetAddress.getLocalHost();
		System.out.println("Mapper : " + ip);
	
		//value String
		String line = value.toString();
		//提取年份
		String year = line.substring(15, 19);
		//气温
		int airTemperature;
		//提取气温值+ / -
		if (line.charAt(87) == '+') { 
			airTemperature = Integer.parseInt(line.substring(88, 92));
		} else {
			airTemperature = Integer.parseInt(line.substring(87, 92));
		}
		
		//质量
		String quality = line.substring(92, 93);
		
		//判断气温的有效性
		if (airTemperature != MISSING && quality.matches("[01459]")) {
			context.write(new Text(year), new IntWritable(airTemperature));
		}
	}

}
